﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Repositories.Core;

namespace ASPNETMVCApplication.Repositories
{
    public class EmployeeRepository : RepositoryBase, IEmployeeRepository
    {
        public List<Employee> GetList(Criterion criterion = null)
        {
            var request = new EmployeeRequest().Prepare();

            request.LoadOptions = new string[] { "Employees" };
            request.Criteria = new EmployeeCriteria
            {
                EmployeeId = int.Parse(criterion.Filters.Single(f => f.Attribute == "EmployeeId").Operand.ToString()),
                SortExpression = criterion.OrderByExpression
            };

            var response = Client.GetEmployees(request);

            Correlate(request, response);

            return response.Employees == null ? null : response.Employees.ToList();
        }
        public List<Employee> Search(int supplierID, int employeeID, string sort, string order)
        {
            var request = new EmployeeRequest().Prepare();

            request.LoadOptions = new string[] { "Search" };

            request.Criteria = new EmployeeCriteria
            {
                EmployeeId = employeeID,
                SupplierId = supplierID,
                SortExpression = sort + " " + order
            };

            var response = Client.GetEmployees(request);

            Correlate(request, response);

            return response.Employees.ToList();
        }
        public Employee Get(int supplierID, int employeeID)
        {
            var request = new EmployeeRequest().Prepare();
            request.LoadOptions = new string[] { "Employee" };
            request.Criteria = new EmployeeCriteria { SupplierId = supplierID, EmployeeId = employeeID };

            var response = Client.GetEmployees(request);

            Correlate(request, response);

            return response.Employee;
        }
        public void Insert(Employee employee)
        {
            var request = new EmployeeRequest().Prepare();
            request.Action = "Create";
            request.Employee = employee;

            var response = Client.SetEmployees(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }
        public void Update(Employee employee)
        {
            var request = new EmployeeRequest().Prepare();
            request.Action = "Update";
            request.Employee = employee;

            var response = Client.SetEmployees(request);

            Correlate(request, response);

            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
        }
        #region Not implemented members
        public Employee Get(int _employeeID)
        {
            throw new NotImplementedException();
        }
        public List<Employee> Search(int _employeeID)
        {
            throw new NotImplementedException();
        }
        public int GetCount(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }
        public void Delete(int employeeId)
        {
            throw new NotImplementedException();
        }
        public void Dispose()
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}